package com.fz.us.oa.web.controller.main;

import com.fz.us.admin.base.utils.Identities;
import com.fz.us.admin.base.utils.LogUtil;
import com.fz.us.oa.web.controller.BaseController;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 控制器 - 登录
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-29 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/main")
public class LoginController extends BaseController {

    private static final long serialVersionUID = -7233017017119091789L;

    private static final String webLoginResult = "/main/login";

    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login(Model model) {

        model.addAttribute("code", Identities.uuid2());

        return webLoginResult;
    }

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model){
        LogUtil.info(username + "|" + password);

        if (SecurityUtils.getSubject().isRemembered() || SecurityUtils.getSubject().isAuthenticated()) {
            return "redirect:/main/index";
        }
        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
        String error = null;
        if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if (ExcessiveAttemptsException.class.getName().equals(exceptionClassName)) {
            error = "密码输入错误超过5次，请10分钟后再试";
        } else if (AuthenticationException.class.getName().equals(exceptionClassName)) {
            error = "您的账号没有访问权限";
        } else if ("jCaptcha.error".equals(exceptionClassName)) {
            error = "验证码错误";
        } else if (exceptionClassName != null) {
            error = "其他错误：" + exceptionClassName;
        }
        model.addAttribute("username",username);
        model.addAttribute("error",error);
        model.addAttribute("code", Identities.uuid2());
        return webLoginResult;
    }

    @RequestMapping(value = "logout", method = RequestMethod.GET)
    public String logout(Model model) {

        return webLoginResult;
    }

    @RequestMapping(value = "qrcodelogin", method = RequestMethod.POST)
    public String qrcodelogin(@RequestParam String uid, @RequestParam String code, Model model){
        if (SecurityUtils.getSubject().isRemembered() || SecurityUtils.getSubject().isAuthenticated()) {
            return "redirect:/main/index";
        }
        String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
        String error = null;
        if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if (ExcessiveAttemptsException.class.getName().equals(exceptionClassName)) {
            error = "密码输入错误超过5次，请10分钟后再试";
        } else if (AuthenticationException.class.getName().equals(exceptionClassName)) {
            error = "您的账号没有访问权限";
        } else if ("jCaptcha.error".equals(exceptionClassName)) {
            error = "验证码错误";
        } else if (exceptionClassName != null) {
            error = "其他错误：" + exceptionClassName;
        }
        model.addAttribute("error",error);
        return webLoginResult;
    }

}
